home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / games / larn12s.arc / TERMCAP.ARC / TESTTCP.C < prev    next >
C/C++ Source or Header  |  1987-10-28  |  7KB  |  349 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  TEST PROGRAM
  22.  *
  23.  *    testtcp   test termcap functions
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    test routines
  28.  *    termcap test
  29.  *
  30.  *  SYNOPSIS
  31.  *
  32.  *    termcap [-efns] terminal [capability [capability ...]]
  33.  *
  34.  *        -e  =>   expand string capability given by -s
  35.  *        -f  =>   determine boolean capabilities for terminal
  36.  *        -n  =>   determine numeric capabilities for terminal
  37.  *        -s  =>   determine string capabilities for terminal
  38.  *
  39.  *        terminal =>  terminal name as given in termcap file
  40.  *        capability => a boolean, numeric, or string capability
  41.  *
  42.  *        NOTE:  All capabilities must be of same type, as
  43.  *               given by [-fns].
  44.  *
  45.  *        If terminal is only argument then entire entry is
  46.  *        printed.
  47.  *
  48.  *  DESCRIPTION
  49.  *
  50.  *    Provides way to test termcap functions.  Can find
  51.  *    and print an entire termcap terminal entry, or various
  52.  *    capabilities from the entry.
  53.  *
  54.  *  AUTHOR
  55.  *
  56.  *    Fred Fish
  57.  *
  58.  */
  59.  
  60. #include <stdio.h>
  61.  
  62. #define TRUE 1
  63. #define FALSE 0
  64. #define NO_FILE     -1            /* Returned if can't open file */
  65. #define NO_ENTRY  0            /* Returned if can't find entry */
  66. #define SUCCESS   1            /* Returned if entry found ok */
  67. #define TRUNCATED 2            /* Returned if entry found but trunc */
  68. #define BUFFER_SIZE 1024
  69.  
  70. int eflag = FALSE;
  71. int fflag = FALSE;
  72. int nflag = FALSE;
  73. int sflag = FALSE;
  74.  
  75. int got_terminal = FALSE;
  76. int got_capability = FALSE;
  77.  
  78.  
  79. /*
  80.  *  FUNCTION
  81.  *
  82.  *    main   termcap test entry point
  83.  *
  84.  *  KEY WORDS
  85.  *
  86.  *    main
  87.  *
  88.  *  SYNOPSIS
  89.  *
  90.  *    main(argc,argv)
  91.  *    int argc;
  92.  *    char *argv[];
  93.  *
  94.  *  DESCRIPTION
  95.  *
  96.  *    This is where the termcap test starts executing.  All argument list
  97.  *    switches are processed first, then all the specified
  98.  *    capability identification strings are processed.
  99.  *
  100.  */
  101.  
  102. /*
  103.  *  PSEUDO CODE
  104.  *
  105.  *    Begin main
  106.  *        Process command line options.
  107.  *        For each argument list field
  108.  *        If field was not erased during option processing
  109.  *            If terminal name field not yet processed then
  110.  *            Process an assumed terminal name field.
  111.  *            Set terminal name processed flag.
  112.  *            Else
  113.  *            Process a capability field.
  114.  *            Set capability field processed flag.
  115.  *            End if
  116.  *        End if
  117.  *        End for
  118.  *        If no capabilities processed then
  119.  *        Simply dump buffer.
  120.  *        End if
  121.  *    End main
  122.  *
  123.  */
  124.  
  125. main(argc, argv)
  126. int argc;
  127. char *argv[];
  128. {
  129.     char *argp;
  130.     int argnum;
  131.     char buffer[BUFFER_SIZE];
  132.  
  133.     options(argc,argv);
  134.     for (argnum = 1; argnum < argc; argnum++) {
  135.         if ((argp = argv[argnum]) != NULL) {
  136.         if (!got_terminal) {
  137.         terminal(buffer,argp);
  138.         got_terminal = TRUE;
  139.         } else {
  140.         capability(argp);
  141.         got_capability = TRUE;
  142.         }
  143.         }
  144.     }
  145.     if (got_terminal && !got_capability) {
  146.     printf("%s",buffer);
  147.     }
  148. }
  149.  
  150. /*
  151.  *  FUNCTION
  152.  *
  153.  *    options   process command line options
  154.  *
  155.  *  SYNOPSIS
  156.  *
  157.  *    options(argc,argv)
  158.  *    int argc;
  159.  *    char *argv[];
  160.  *
  161.  *  DESCRIPTION
  162.  *
  163.  *    Scans argument list, processing each switch as it is
  164.  *    found.  The pointer to each switch string is then
  165.  *    replaced with a NULL to effectively erase the switch
  166.  *    argument.
  167.  *
  168.  */
  169.  
  170. /*
  171.  *  PSEUDO CODE
  172.  *
  173.  *    Begin options
  174.  *        For each argument in the argument list
  175.  *        Get pointer to first char of argument.
  176.  *        If the argument is a switch then
  177.  *            Replace argument pointer with NULL.
  178.  *            Look at next argument character.
  179.  *            While there is another argument character
  180.  *            Switch on the argument character
  181.  *            Case "EXPAND":
  182.  *                Set expand (e) flag.
  183.  *                Break out of switch.
  184.  *            Case "BOOLEAN":
  185.  *                Set boolean (f) flag.
  186.  *                Break out of switch.
  187.  *            Case "NUMERIC":
  188.  *                Set numeric flag.
  189.  *                Break out of switch.
  190.  *            Case "STRING":
  191.  *                Set string flag.
  192.  *                Break out of switch.
  193.  *            Default:
  194.  *                Abort with usage message.
  195.  *            End switch
  196.  *            End while
  197.  *        End if
  198.  *        End for
  199.  *    End options
  200.  *
  201.  */
  202.  
  203. options(argc, argv)
  204. int argc;
  205. char *argv[];
  206. {
  207.     int i;
  208.     char c;        /* 1st char of current command-line argument */
  209.     char *cp;        /* current argument pointer */
  210.  
  211.     for (i=1; i<argc; i++) {
  212.         cp = argv[i];
  213.         if (*cp == '-') {
  214.             argv[i] = NULL;
  215.         cp++;
  216.         while (c = *cp++) {
  217.             switch (c) {
  218.         case 'e':
  219.             eflag = TRUE;
  220.             break;
  221.         case 'f':
  222.             fflag = TRUE;
  223.                 break;
  224.             case 'n':
  225.             nflag = TRUE;
  226.                 break;
  227.             case 's':
  228.             sflag = TRUE;
  229.                 break;
  230.             default:
  231.                 usage();
  232.             }
  233.             }
  234.         }
  235.     }
  236. }
  237.  
  238. /*
  239.  *  FUNCTION
  240.  *
  241.  *    usage   give usage message and abort
  242.  *
  243.  *  KEY WORDS
  244.  *
  245.  *    usage
  246.  *    help processing
  247.  *    abort locations
  248.  *
  249.  *  SYNOPSIS
  250.  *
  251.  *    usage()
  252.  *
  253.  *  DESCRIPTION
  254.  *
  255.  *    Usage is typically called when a problem has been
  256.  *    detected in the argument list.
  257.  *    It prints a usage message and exits.
  258.  *
  259.  */
  260.  
  261. /*
  262.  *  PSEUDO CODE
  263.  *
  264.  *    Begin usage
  265.  *        Print usage message.
  266.  *        Exit.
  267.  *    End usage
  268.  *
  269.  */
  270.  
  271. usage()
  272. {
  273.     printf("Usage: termcap [-fns] terminal [capability [capability ... ]]\n");
  274.     exit();
  275. }
  276.  
  277.  
  278. terminal(buffer,name)
  279. char *buffer;
  280. char *name;
  281. {
  282.     int status;
  283.  
  284.     status = tgetent(buffer,name);
  285.     switch (status) {
  286.     case NO_FILE:
  287.     fprintf(stderr,"Can't find a termcap data base file.\n");
  288.     exit();
  289.     case NO_ENTRY:
  290.     fprintf(stderr,"Can't find entry \"%s\"\n",name);
  291.     exit();
  292.     case TRUNCATED:
  293.     fprintf(stderr,"Warning --- entry \"%s\" too long\n",name);
  294.     break;
  295.     case SUCCESS:
  296.         break;
  297.     default:
  298.         fprintf(stderr,"? tgetent returned illegal status %d\n",status);
  299.     exit();
  300.     }
  301. }
  302.  
  303. capability(id)
  304. char *id;
  305. {
  306.     int value;
  307.     char buffer[256];
  308.     char *area;
  309.     char *ep, *tgoto();
  310.  
  311.     if (fflag) {
  312.     value = tgetflag(id);
  313.     if (value) {
  314.         printf("%s TRUE\n",id);
  315.     } else {
  316.         printf("%s FALSE\n",id);
  317.     }
  318.     } else if (nflag) {
  319.     value = tgetnum(id);
  320.     printf("%s = %o octal %d decimal\n",id,value,value);
  321.     } else if (sflag) {
  322.     area = buffer;
  323.     tgetstr(id,&area);
  324.     if (eflag) {
  325.         ep = tgoto(buffer,75,23);
  326.     }
  327.     doprint(id,buffer);
  328.     if (eflag) {
  329.         doprint(id,ep);
  330.     }
  331.     }
  332. }
  333.  
  334. doprint(id,cp)
  335. char *id;
  336. char *cp;
  337. {
  338.     printf("%s = ",id);
  339.     for ( ; *cp != NULL; cp++) {
  340.     if (*cp < 040) {
  341.         printf("^%c ",*cp |= 0100);
  342.     } else {
  343.         printf("%c ",*cp);
  344.     }
  345.     }
  346.     printf("\n");
  347. }
  348.  
  349.